package com.example.cpms.service.impl;

import com.example.cpms.entity.Device;
import com.example.cpms.mapper.DeviceMapper;
import com.example.cpms.service.DeviceService;
import com.example.cpms.utils.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {


    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public void delete(Integer id) {
        deviceMapper.deleteById(id);
    }

    @Override
    public void add(Device device) {
        device.setStatus("有空闲");
        deviceMapper.insert(device);
    }

    @Override
    public void update(Device device) {
        deviceMapper.update(device);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);//使用PageHelper分页查询插件
        List<Device> deviceList = deviceMapper.list(name); //从数据库获取列表数据
        Page<Device> p = (Page<Device>) deviceList; //强转
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public List<Device> getALLDevice() {
        return deviceMapper.findAll();
    }
}
